package jwdt.xadmin.work.service.impl;

import java.util.List;

import jwdt.common.dao.BaseDao;
import jwdt.common.util.NewObjectUtil;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TTask;
import jwdt.xadmin.work.bean.TTaskLog;
import jwdt.xadmin.work.service.TaskLogManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;

public class TaskLogManageServiceImpl implements TaskLogManageService{

	private BaseDao<TTaskLog> baseDao;
	
	public void setBaseDao(BaseDao<TTaskLog> baseDao) {
		this.baseDao = baseDao;
	}
	@Override
	public PageUtil doList(PageUtil pu,int taskid) throws Exception{
		String where = " taskid = "+taskid;
		String sql =where+ " order by dt desc";
		pu.doList(TTaskLog.class,sql);
		//创造json数据对象
		return pu;
	}

	@Override
	public PageUtil doList(PageUtil pu, TUser userInfo, TTaskLog obj,int projectId)
			throws Exception {
		String where = "1=1";
		if(projectId !=0){
			where +=" and EXISTS (SELECT taskid FROM t_task t WHERE EXISTS(SELECT projectid FROM t_project p WHERE t.projectid=p.projectid AND (p.projectid="+projectId+" OR p.fid="+projectId+"))  AND t_tasklog.taskid = t.taskid)";
		}
		String dt = obj.getDt();
		String demo = obj.getDemo();
		if(!StringUtil.nullOrBlank(dt) && !StringUtil.nullOrBlank(demo)){
			where += " AND (dt BETWEEN '"+dt+"' AND '"+demo+"')";
		}
		if(userInfo != null) {
			where += " and userid="+userInfo.getUserid();
		}
		Integer zt = obj.getZt();
		if(zt!=null){
			where += " and zt="+zt;
		}
		String sql =where+ " order by dt desc";
		pu.doList(TTaskLog.class,sql);
		//创造json数据对象
		return pu;
	}

	@Override
	public String doAddact(TTaskLog obj,TUser userInfo) throws Exception {
		obj.setDt(DateUtil.getDateTime());
		String ret = baseDao.doAddObj(obj);
		searchAndUpdateTaskState(obj.getTaskid());
		return ret;
	}

	@Override
	public String doEditact(TTaskLog obj, int id) throws Exception {
		String ret = baseDao.doUpdateObj(TTaskLog.class, id, obj);
		searchAndUpdateTaskState(obj.getTaskid());
		return ret;
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		String ret = baseDao.doDelObj(TTaskLog.class, id);
		return ret;
	}

	@Override
	@SuppressWarnings("unchecked")
	public TTaskLog doLoad(int taskid,boolean flag) throws Exception {
		String sql = " logid=";
		if(flag){
			sql = " taskid=";
		}
		List<TTaskLog> list = Jdb.get().query(TTaskLog.class, sql+taskid+" order by dt desc", 1);
		return  list.isEmpty()?(TTaskLog)NewObjectUtil.getNewObject(TTaskLog.class):list.get(0);
	}

	@Override
	public String doUpdateState(int id, String result) throws Exception {
		String ret = baseDao.doIntUpdateState(TTaskLog.class, "logid="+id, result);
		TTaskLog taskLog = (TTaskLog) Jdb.get().get(TTaskLog.class, id);
		searchAndUpdateTaskState(taskLog.getTaskid());
		return ret;
	}

	@Override
	public String doBackTask(TTaskLog tasklog,TUser userInfo) throws Exception {
		TTaskLog oldLog = (TTaskLog) Jdb.get().get(TTaskLog.class, tasklog.getLogid());
		oldLog.setName("退回："+oldLog.getName());
		oldLog.setZt(2);
		String ret = baseDao.doUpdateObj(TTaskLog.class, tasklog.getLogid(), oldLog);
		if("修改成功".equals(ret)){
			tasklog.setLogid(null);
			tasklog.setFromuser(userInfo.getUserid());
			tasklog.setDt(DateUtil.getDateTime());
			tasklog.setZt(0);
			tasklog.setTaskid(oldLog.getTaskid());
			ret = baseDao.doAddObj(tasklog);
			if("添加成功".equals(ret)){
				ret = "操作成功";
			}
		}else{
			ret = "操作失败";
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	public void searchAndUpdateTaskState(int taskid){
		try {
			int count = Jdb.get().count(TTaskLog.class, "zt=2 and taskid="+taskid);
			List<TTaskLog> list = Jdb.get().query(TTaskLog.class," taskid="+taskid);
			if(count == list.size()){
				baseDao.doIntUpdateState(TTask.class, "taskid="+taskid, "zt=3");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
